﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using CSM.Controllers;
using System.Web.SessionState;
using CSM.Models;

namespace CSM.ashx
{
    public class Submit : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            CustomDataContext db = new CustomDataContext();
            string customid = "";
            if (context.Session[SystemVars.sysSessionCustomId] != null)
            {
                customid = context.Session[SystemVars.sysSessionCustomId].ToString();
            }
            string foruser = context.Request["Email"];
            string StrAction = context.Request["action"];
            string Textstring = context.Request["TeString"];
            string WjType = context.Session[SystemVars.sysSessionWjType].ToString();
            string[] options = foruser.Split('%');
            string[] textstring = Textstring.Split('%');
            if (StrAction == "action")
            {
                if (foruser != null)
                {
                    int i = 1;
                    string defaultid = (from a in db.TCrm_Sys_Items where a.ItemID == WjType.Trim() select a.DefaultValue).Single();
                    string[] questions = (from a in db.TCrm_WJ_Statistics_Detail where a.StatID == defaultid select a.QuestionID).Single().Split(',');
                    var MyQuery = from a in db.TCrm_WJ_CustomerAnswer
                                  where a.CustomerID == customid && a.StatID == defaultid//查找该用户，该问卷
                                  select a;
                    //以前保存的数据删除
                    if (MyQuery.Count() > 0)
                    {
                        db.TCrm_WJ_CustomerAnswer.DeleteAllOnSubmit(MyQuery);
                        db.SubmitChanges();
                    }
                    string datetime = DateTime.Now.ToString();
                    foreach (var questionid in questions)
                    {
                        TCrm_WJ_CustomerAnswer myrow = new TCrm_WJ_CustomerAnswer();
                        myrow.QuestionID = questionid;
                        myrow.StatID = defaultid;
                        myrow.OptionID = options[i];
                        myrow.Txt = textstring[i];
                        myrow.ShopID = (from a in db.TCrm_KF_Customer where a.CustomerID == customid select a.ShopID).Single().ToString();
                        myrow.UserID = context.Session[SystemVars.sysSessionUserId].ToString();
                        myrow.AnswerTime = Convert.ToDateTime(datetime);
                        myrow.CustomerID = customid;
                        db.TCrm_WJ_CustomerAnswer.InsertOnSubmit(myrow);
                        db.SubmitChanges();
                        i++;
                    }
                    var query = (from a in db.TCrm_KF_Customer where a.CustomerID == customid select a).Single();
                    query.IsLock = false;
                    db.SubmitChanges();
                    context.Response.Clear();
                    context.Response.ContentType = "application/text";
                    context.Response.Write("yes");
                    context.Response.End();
                }
                else
                {
                    context.Response.Clear();
                    context.Response.ContentType = "application/text";
                    context.Response.Write("no");
                    context.Response.End();
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
